const express = require('express')

const bodyParser = require('body-parser')

const db = require('./db')

const app = express()
const port = 6648

app.use(bodyParser.json())

app.all("*",(req,res,next)=>{ 
        res.header("Access-Control-Allow-Origin","*") 
        res.header("Access-Control-Allow-Headers","Content-Type") 
        res.header("Access-Control-Allow-Methods","*") 
        res.header('Content-Type','application/json;charset=utf-8') 
        next() 
    }) 

app.get('/getLocation',(req,res)=>{
    
    const {shopCode} = req.query
    const query = `SELECT * FROM delvery_location where shopCode = ${shopCode}`
    
    db.getConnection((err,connection)=>{
        if(err){
            return res.status(200).json({
                code:-1,
                msg:'获取位置失败'
            })
        }
        
        connection.query(query,(err,result)=>{
        if(err){
            return res.status(200).json({
                code:-1,
                msg:'获取位置失败'
            })
        }
        if(result.length>0){
            console.log("获取到了",result)
            res.status(200).json({
                code:0,
                data:result.length>0?result[0]:{}
            })
        }else{
            res.status(200).json({
                code:-1,
                msg:'没有找到该门店位置'
            })
        }
    })
    })
})

app.listen(port,()=>{
    console.log('运行查询经纬度成功');
    
})